package com.qiyeweixin.Page;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;

/**
 * app启动界面
 */
public class App extends BasePage{

    public static final String appPackage = "com.tencent.wework";
    public static final String appActivity = ".launch.LaunchSplashActivity";

    /**
     * 空构造函数 不需要初始化driver
     */
    public App(){
        super();
    }

    /**
     * 启动appium，生成driver，表示一次自动化过程
     * @return MainPage MainPage
     * @throws MalformedURLException MalformedURLException
     */
    public MainPage start() throws MalformedURLException {
        logger.info("程序开始运行，正在加载配置！");
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("noReset", "true"); //不重置环境 可保存登录信息
        desiredCapabilities.setCapability("appPackage", appPackage);
        desiredCapabilities.setCapability("appActivity", appActivity);
        desiredCapabilities.setCapability("deviceName", "yy");

        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        logger.info("App启动中，请勿手动操作界面！");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); //隐式等待

        return new MainPage(driver);
    }

    public void close(){
        try {
            logger.info("测试程序已完成，准备关闭app！");
            driver.close();
        } catch (Exception e) {
            logger.info("警告！警告！警告！测试程序关闭异常！错误信息："+e);
        }
    }
}
